<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* ****************************************************************************
  NAIYUMIE KING WANG JJANG Front End Developer <naiyumie@gmail.com>
  FileType:CRLF, Encording:UTF-8, Tab&Space:4/4
  ------------------------------------------------------------------------
  모델
**************************************************************************** */
class N_Model extends CI_Model {
    function __construct() {
        parent::__construct();
	}

	/***************************************************
	 * model and value objects
	 ***************************************************/
	function m01(){
		$sql = "SHOW FULL COLUMNS FROM `tbl_members`;";
		$query = $this->db->query($sql);
		$result = $query->result_array($query);
		$retVal = $result;
		return $retVal;
	}

	function o01(){
		$query = $this->db->query("SELECT `text` , `code`, `isAbsolute` FROM tbl_enum;");
		//nlog($query);
		$result = $query->result_array();
		$retVal = $result;
		return $retVal;
	}


	# enum의 set을 배열로 반환
	public function getEnumColumn($argArray=array()){
		$sql = sprintf("SHOW COLUMNS FROM `%s` LIKE '%s';", $argArray['fg_tableName'], $argArray['columnName']);
		$columns = $this->db->query($sql)->row_array();
		$enumSet = $columns['Type'];
		$enumSet = str_replace('enum(','', $enumSet);
		$enumSet = str_replace(')','', $enumSet);
		$enumSet = str_replace("'",'', $enumSet);
		$enumArray = explode(',', $enumSet);
		//echo $enumSet;
		//nPrt($enumArray);
		//nPrt($columns);
		$retVal = $enumArray;
		return $retVal;
	}

	# enum의 default값을 반환
	public function getEnumDefault($argArray=array()){
		$sql = sprintf("SHOW COLUMNS FROM `%s` LIKE '%s';", $argArray['fg_tableName'], $argArray['columnName']);
		$columns = $this->db->query($sql)->row_array();
		$enumSet = $columns['Default'];
		$retVal = $enumSet;
		return $retVal;
	}

	# 배열을 SET 스태이트먼트로 변환
	function getQuerySetStateMent($argArray){
		$querySetStatment = '';
		$i = 0;
		foreach($argArray as $key=>$val){
			if($i != 0){
				$querySetStatment .= ',';
			}
			$querySetStatment .= $key . '=' . "'". $val . "'";
			$i ++;
		}
		$querySetStatment .= ' ';
		$retVal = $querySetStatment;
		return $retVal;

	}


	# unique를 체크 한다.
	function isUnique($argArray = array()){
		$sql = sprintf('
			SELECT
				COUNT(%2$s) AS cnt
			FROM
				%1$s
			WHERE %2$s = "%3$s"
				AND pk_uid != "%4$s" ;',
			$argArray['uq_tableName'], $argArray['columnName'], $argArray['columnValue'], $argArray['columnPk']
		);
		$result = $this->db->query($sql)->result_array();
		$count = $result[0]['cnt'];
		if($count > 0){
			return FALSE;
		} else {
			return TRUE;
		}
	}
}
/* EOF */